<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;

class OrderController extends Controller
{
    /**
     * 生成订单
     * @param Request $request
     * @return \Illuminate\Http\JsonResponse
     */
    public function insert(Request $request) {
        $data = $request->all();

        $request->session()->forget("detail");

        $id = DB::table("orders")->insertGetId(
            [
                'detail_id' => $data['detail_id'],
                'user_email' => $data['user_email'],
                'business_id' => $data['bussiness_id'],
                'address_id' => $data['address_id'],
                'payway' => $data['payway'],
                'sendway' => $data['sendway'],
                'get_time' => $data['get_time'],
                'ordercode' => time(),
                'order_bak' => $data['order_bak'],
                'created_at' => date('Y-m-d H:i;s', time())
            ]
        );

        DB::table("details")->where("id", $data['detail_id'])->delete();
        return response()->json($id);
    }

    public function complete($order_id){

        $res = DB::table('orders')->select('id', 'user_email', 'business_id', 'address_id', 'get_time')
            ->where('id', $order_id)
            ->first();

        $business_info = DB::table('businesses')
            ->select('id', 'business_address', 'username', 'send_costs', 'start_send_costs')
            ->where('id', '=', $res->business_id)->first();

        $address_info = DB::table('addresses')
            ->select('id', 'getname', 'sex', 'address', 'position', 'phone')
            ->where('id', '=', $res->address_id)->first();

        return view('home.order.complete', [
            'business_info' => $business_info,
            'user_info' => $res->user_email,
            'address_info' => $address_info,
            'get_time' => $res->get_time
        ]);
    }
}
